/*
 * @Author: xiaosihan 
 * @Date: 2024-08-01 15:32:26 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-09-26 21:45:28
 */

import shortid from 'shortid';
import { MeshBasicMaterial, MeshLambertMaterial, MeshPhongMaterial, MeshPhysicalMaterial, MeshStandardMaterial } from 'three';
import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js';
import pathBrowserify from "path-browserify";

class Utils {

    constructor() {
    }



    // 开发环境
    isDev = (() => {
        return (process.env.NODE_ENV === "development");
    })();

    // 测试环境
    isTest = (() => {
        return (process.env.NODE_ENV === "test");
    })();

    // 正式环境
    isProd = (() => {
        return (process.env.NODE_ENV === "production");
    })();

    // 生成短ID
    uuid() {
        const shortId = shortid();
        return shortId;
    }

    // 解析URL路径
    parseUrl(url: string) {
        const { root, dir, base, ext, name, } = pathBrowserify.parse(url);
        return { root, dir, base, ext, name };
    }

    // 编辑材质
    editorMaterial(material: MeshBasicMaterial | MeshLambertMaterial | MeshPhongMaterial | MeshPhysicalMaterial | MeshStandardMaterial) {
        const config: { [key: string]: any } = {};

        const controlPanel = new GUI({ width: 400 });

        controlPanel.title('材质编辑器');
        controlPanel.show(true);

        //颜色修改
        controlPanel
            .addColor(material as MeshStandardMaterial, 'color')
            .name('材质颜色')
            .onChange((color) => {
                config['color'] = `#${color.getHexString()}`;
            });

        //金属性
        if (material.hasOwnProperty('metalness')) {
            controlPanel
                .add(material as MeshStandardMaterial, 'metalness', 0, 1, 0.01)
                .name('金属性')
                .onChange((metalness) => {
                    config['metalness'] = metalness;
                });
        }
        //粗糙度
        if (material.hasOwnProperty('roughness')) {
            controlPanel
                .add(material as MeshStandardMaterial, 'roughness', 0, 1, 0.01)
                .name('粗糙度')
                .onChange((roughness) => {
                    config['roughness'] = roughness;
                });
        }

        function stringifyWithoutQuotes(obj: any) {
            let result = '';
            function stringifyValue(value: any) {
                if (typeof value === 'string') {
                    return `"${value}"`; // 字符串值需要引号
                } else if (typeof value === 'object' && value !== null) {
                    // 如果值是对象或数组，递归处理（这里简单返回字符串表示）
                    return '...'; // 或者您可以递归调用 stringifyWithoutQuotesOnKeys(value) 并处理嵌套
                } else {
                    // 对于其他类型（如数字、布尔值、null），直接返回其字符串表示
                    return String(value);
                }
            }

            for (let key in obj) {
                if (obj.hasOwnProperty(key)) {
                    result += `${key}: ${stringifyValue(obj[key])}, `;
                }
            }
            // 移除末尾的逗号和空格
            return result.trim().slice(0, -1);
        }

        controlPanel
            .add(
                {
                    click: () => {
                        navigator.clipboard.writeText(stringifyWithoutQuotes(config));
                        controlPanel.destroy();
                    },
                },
                'click'
            )
            .name('确定,并复制配置到剪贴板');
    }

}
// Utils类结束

// 创建Utils实例
const utils = new Utils();
// 导出utils实例
export default utils;